package net.contrend.system.dao;

import java.sql.Types;
import java.util.List;
import java.util.UUID;

import org.springframework.jdbc.core.JdbcTemplate;

import net.contrend.system.form.MenuForm;
import net.contrend.system.mapper.MenuFormMapper;
import net.contrend.util.SqlUtils;
import net.contrend.util.StringUtils;

public class MenuDao extends BaseDao {
    
    private String table = "ct_menu";
    
    public MenuDao() {
    }
    
    public MenuDao(JdbcTemplate jt) {
        this.jt = jt;
    }
    
    public int insert(MenuForm menu) {
        String columns = "menuid, menuname, menuurl, parentid, status, orderno, remark";
        String sql = SqlUtils.getInsertSql(this.table, columns);
        System.out.println(String.format("\n%1$s\n", sql));
        
        if (StringUtils.isBlank(menu.getMenuId())) {
            menu.setMenuId(UUID.randomUUID().toString());
        }
        
        return this.jt.update(sql,
                new Object[]{
                    menu.getMenuId(),
                    menu.getMenuName(),
                    menu.getMenuUrl(),
                    menu.getParentId(),
                    menu.getStatus(),
                    menu.getOrderNo(),
                    menu.getRemark()
                },
                new int[]{
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.INTEGER,
                    Types.VARCHAR
                });
    }
    
    public int update(MenuForm menu) {
        String columns = "menuname, menuurl, parentid, status, orderno, remark";
        String sql = SqlUtils.getUpdateSql(this.table, columns, "menuid = ?");
        System.out.println(String.format("\n%1$s\n", sql));
        
        return this.jt.update(sql,
                new Object[]{
                    menu.getMenuName(),
                    menu.getMenuUrl(),
                    menu.getParentId(),
                    menu.getStatus(),
                    menu.getOrderNo(),
                    menu.getRemark(),
                    menu.getMenuId()
                },
                new int[]{
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.INTEGER,
                    Types.VARCHAR,
                    Types.VARCHAR
                });
    }
    
    public int delete(String id) {
        String sql = SqlUtils.getDeleteSql(this.table, "menuid = ?");
        System.out.println(String.format("\n%1$s\n", sql));
        return this.jt.update(sql, new Object[]{id}, new int[]{Types.VARCHAR});
    }
    
    @SuppressWarnings("unchecked")
    public MenuForm query(String id) {
        String sql = "select * from ct_menu where menuid = ?";
        List<MenuForm> menuList = this.jt.query(sql,
                new Object[]{id}, new int[]{Types.VARCHAR}, new MenuFormMapper());
        if (null != menuList && 0 < menuList.size()) {
            return menuList.get(0);
        }
        return null;
    }
    
    public List<MenuForm> getMenus() {
        return this.getMenus(true);
    }
    
    @SuppressWarnings("unchecked")
    public List<MenuForm> getMenus(boolean isValid) {
        String sql = "select m.* from ct_menu m left join ct_menu m2 on m2.menuid = m.parentid where 1 = 1 %1$s order by m2.orderno, m.orderno";
        sql = String.format(sql, isValid ? "and m.status = '1'" : "");
        System.out.println(String.format("\n%1$s\n", sql));
        List<MenuForm> menuList = this.jt.query(sql, new MenuFormMapper());
        return menuList;
    }
    

    public List<MenuForm> getMenuChildren(String parentId) {
        return this.getMenus(true);
    }
    
    @SuppressWarnings("unchecked")
    public List<MenuForm> getMenuChildren(String parentId, boolean isValid) {
        String sql = "select m.* from ct_menu m left join ct_menu m2 on m2.menuid = m.parentid where 1 = 1 and m.parentid = ? %1$s order by m2.orderno, m.orderno";
        sql = String.format(sql, isValid ? "and m.status = '1'" : "");
        System.out.println(String.format("\n%1$s\n", sql));
        List<MenuForm> menuList = this.jt.query(sql, new Object[]{parentId}, new int[]{Types.VARCHAR}, new MenuFormMapper());
        return menuList;
    }
    
}
